package cn.edu.zut.score.service.impl;

import cn.edu.zut.score.core.cache.RedisCache;
import cn.edu.zut.score.core.constant.GroupInfoEnum;
import cn.edu.zut.score.core.domain.RestResponse;
import cn.edu.zut.score.core.entity.GroupInfo;
import cn.edu.zut.score.core.entity.Info;
import cn.edu.zut.score.core.entity.User;
import cn.edu.zut.score.mapper.GroupInfoMapper;
import cn.edu.zut.score.service.IGroupInfoService;
import cn.edu.zut.score.service.IGroupService;
import cn.edu.zut.score.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-06-25
 */
@Service
public class GroupInfoServiceImpl extends ServiceImpl<GroupInfoMapper, GroupInfo> implements IGroupInfoService {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private RedisCache redisCache;

    @Autowired(required = false)
    private GroupInfoMapper groupInfoMapper;

    @Autowired
    private IGroupService iGroupService;

    @Autowired
    private IUserService iUserService;

    @Override
    public List<Info> getGroupNumbers(Long groupId) {
        LambdaQueryWrapper<GroupInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GroupInfo::getGroupId, groupId);
        qw.orderByAsc(GroupInfo::getType, GroupInfo::getId);
        List<Long> ids = groupInfoMapper.selectList(qw).stream().map(GroupInfo::getUserId).collect(Collectors.toList());
        return iUserService.queryUserNamesByIds(ids);
    }

    @Override
    public RestResponse<List<Info>> queryAllLeadersByRole(GroupInfoEnum groupInfoEnum) {
        LambdaQueryWrapper<GroupInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GroupInfo::getType, groupInfoEnum.getCode());
        List<Long> ids = groupInfoMapper.selectList(qw).stream().map(GroupInfo::getUserId).collect(Collectors.toList());
        return RestResponse.success(iUserService.queryUserNamesByIds(ids));
    }

    @Override
    public RestResponse<List<Info>> queryAllRemainTeachers(Long groupid) {
        LambdaQueryWrapper<GroupInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GroupInfo::getGroupId, groupid);
        qw.select(GroupInfo::getUserId);
        List<Long> ids = groupInfoMapper.selectList(qw).stream().map(GroupInfo::getUserId).collect(Collectors.toList());
        return RestResponse.success(iUserService.queryUserInfosNotInIds(ids));
    }

    @Override
    public Integer removeByGroupId(Long groupid) {
        LambdaQueryWrapper<GroupInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GroupInfo::getGroupId, groupid);
        return groupInfoMapper.delete(qw);
    }

    @Override
    public RestResponse<List<Info>> getAllGroups() {
        String token = httpServletRequest.getHeader("Authorization");
        User currentUser = redisCache.getCurrentUser(token);
        Long id = currentUser.getId();
        LambdaQueryWrapper<GroupInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(GroupInfo::getUserId, id);
        List<Long> groupIds = groupInfoMapper.selectList(qw).stream().map(GroupInfo::getGroupId).collect(Collectors.toList());
        return RestResponse.success(iGroupService.getGroupsByIds(groupIds));
    }
}
